import time
import hmac
import hashlib
import base64
import urllib
import json
import sys
import requests
from requests_toolbelt import MultipartEncoder

#飞书机器人链接
URL = "https://open.feishu.cn/open-apis/bot/v2/hook/f592617a-e0b4-4830-b9c7-a2925fa98c06"

def send_msg(msg):
    url = URL
    headers = {"Content-Type": "application/json"}
    response = requests.post(url, json=msg, headers=headers).json()
    print(json.dumps(response, ensure_ascii=False))

push_reason = {
    "PushReview":"新版本进入提审状态",
    "PushOnline":"新版本推送上线",
    "PushUpgrade":"开启新版本强更",
}
def send_push_notify(app_name, platform, build_num, game_num, env, push_name):
    game_num = util.GetPatchGameVersion(platform, game_num, env)
    msg = {
        "msg_type": "post",
        "content": {
            "post": {
                "zh_cn": {
                    "title": "线上推送操作【{}】【{}】{}(游戏版本号:{})".format(app_name, platform, env, game_num),
                    "content": [
                        [
                            {
                                "tag": "text",
                                "text": "\n{}\n".format(push_reason[push_name])
                            }
                        ]
                    ]
                }
            }
        }
    }
    print(msg)
    send_msg(msg)

def send_patch_notify(app_name, platform, build_num, game_num, env, change_log_list):
    game_num = util.GetPatchGameVersion(platform, game_num, env)
    msg = {
        "msg_type": "post",
        "content": {
            "post": {
                "zh_cn": {
                    "title": "【{}】【{}】热腾腾的{}第【{}】(游戏版本号:{})Patch".format(app_name, platform, env, build_num, game_num),
                    "content": [
                        [
                            {
                                "tag": "text",
                                "text": "\n\n修改记录:\n"+(' '.join(change_log_list)).replace("\\n", "\n")
                            }
                        ]
                    ]
                }
            }
        }
    }
    print(msg)
    send_msg(msg)

def send_apk_link(app_name, link, platform, build_num, change_log_list):
    msg = {
        "msg_type": "post",
        "content": {
            "post": {
                "zh_cn": {
                    "title": "【{}】【{}】热腾腾的第【{}】号包".format(app_name, platform, build_num),
                    "content": [
                        [
                            {
                                "tag": "a",
                                "text": "\n"+link.split('/')[-1],
                                "href": link
                            },
                            {
                                "tag": "text",
                                "text": "\n\n修改记录:\n"+(' '.join(change_log_list)).replace("\\n", "\n")
                            }
                        ]
                    ]
                }
            }
        }
    }
    print(msg)
    send_msg(msg)

if __name__ == "__main__":
    argvs = sys.argv

    # argvs[1] 类型
    if argvs[1] == "BuildApp":
        # argvs[2] 包名
        # argvs[3] apk路径
        # argvs[4] 平台
        # argvs[5] 构建号
        # argvs[6] 修改记录
        send_apk_link(argvs[2], argvs[3], argvs[4], argvs[5], argvs[6:])
    elif argvs[1] == "BuildPatch":
        # argvs[2] 包名
        # argvs[3] 平台
        # argvs[4] 构建号
        # argvs[5] 游戏版本号
        # argvs[6] env
        # argvs[7] 修改记录
        send_patch_notify(argvs[2], argvs[3], argvs[4], argvs[5], argvs[6], argvs[7:])
    else:
        # argvs[2] 包名
        # argvs[3] 平台
        # argvs[4] 构建号
        # argvs[5] 游戏版本号
        # argvs[6] env
        send_push_notify(argvs[2], argvs[3], argvs[4], argvs[5], argvs[6], argvs[1])
